import sublime, sublime_plugin
import sys,ctypes,re

class EteldebugCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		try:
			libPath="E:\\vc\\EtelDebug\\Debug\\EtelDebug.dll"
			lib = ctypes.WinDLL(libPath)

			text=self.view.substr(sublime.Region(0,self.view.size()))
			codes=re.split(r'\n\-+\w*\-*\n',text)
			code=codes[0]
			codeLength=len(code)

			debugInfoP=lib.getDebug(code,codeLength)
			debugInfo=ctypes.wstring_at(debugInfoP)

			startLoc=codeLength
			startLoc=startLoc+len(re.match(r'\n\-+\w*\-*\n',self.view.substr(sublime.Region(startLoc,self.view.size()))).group(0))
			self.view.replace(edit,sublime.Region(startLoc,self.view.size()),debugInfo)

			lib.releaseDebug()
			ctypes.windll.kernel32.FreeLibrary(lib._handle)
		except:
			print("Error: ",sys.exc_info()[1])
